<?php
/**
* $Id: edit_banmian.php 171 2010-10-22 09:04:17Z bwanglei@gmail.com $
* 版面帖子/主题管理
*/

include_once(dirname(__FILE__).'/init.php');

// 处理加精和删除操作
if ($_SERVER['REQUEST_METHOD']=='POST') {
	// 现在的全部加精的主题ID号
	if (isset($_POST['is_jing_checkbox'])) {
		$jings_id = $_POST['is_jing_checkbox'];
	} else {
		$jings_id = array();
	}
	// 已经加精的主题ID号
	if (isset($_POST['old_jing'])) {
		$old_jings_id = $_POST['old_jing'];
	} else {
		$old_jings_id = array();
	}
	// 设置要删除的主题ID号
	if (isset($_POST['del'])) {
		$del_ids = $_POST['del'];
	} else {
		$del_ids = array();
	}
	// 设置要加精的主题ID号
	if (isset($_POST['is_top_checkbox'])) {
		$top_ids = $_POST['is_top_checkbox'];
	} else {
		$top_ids = array();
	}
	
	$do_topids = array_diff($top_ids,$del_ids);	// 取得要添加为置顶帖子的ID号
	// 修改数据库添加置顶主题
	foreach ($do_topids as $id) {
		$query = 'update `apf_thread` set `is_top`=1 where `id`='.$id;
		$mysql_conn->query($query);
	}
	// 取得两个数组的差集
	$diff_ids_a = array_diff($old_jings_id,$jings_id);
	$diff_ids_b = array_diff($jings_id,$old_jings_id);
	$diff_ids = array_merge($diff_ids_a,$diff_ids_b);
	$diff_ids = array_diff($diff_ids,$del_ids);	// 减去要删除的主题ID

	// 判断某个ID需要的操作进行数据库操作
	foreach ($diff_ids as $id) {
		if (in_array($id,$jings_id)) {
			$query = 'update `apf_thread` set `is_jing`=1 where `id`='.$id;
		} else if (in_array($id,$old_jings_id)) {
			$query = 'update `apf_thread` set `is_jing`=0 where `id`='.$id;
		}
		$mysql_conn->query($query);
		if ($mysql_conn->affected_rows!=1) apf_msg('修改错误！');
	}

	// 删除主题和主题下的所有帖子
	foreach ($del_ids as $id) {
		$del_thread_r = apf_del_thread($id);
		if ($del_thread_r===false) {
			apf_msg('删除失败',apf_full_url());
			die();
		}
	}
	
	// 删除某些帖子
	if (isset($_POST['del_articles']) && !empty($_POST['del_articles'])) {
		foreach ($_POST['del_articles'] as $del_aid) {
			$query = 'delete from `apf_article` where `id`='.$del_aid;
			$mysql_conn->query($query);
			//if ($mysql_conn->affected_rows!=1) apf_msg('删除帖子遇到错误！');
		}
	}
}

// 获取当前管理员所管理的所有版面
$is_manager = apf_is_manager();
if (!$is_manager) {
	apf_msg('你不是管理员！');die();
} else if ($is_manager=='root') {
	$banmians = apf_get_banmians();
} else {
	$banmians = apf_get_banmians($is_manager);
}

// 获取所有的帖子
if (isset($_GET['bid'])) {
	// 获取当前页码
	$per_count = 15;
	$count_page = apf_edit_banmian_count($_GET['bid'],$per_count);
	if (isset($_GET['page'])) {
		$num_page = intval($_GET['page']);
		if ($numn_page<0) $num_page=0;
		if ($num_page>($count_page-1)) $num_page = ($count_page-1);
	} else {
		$num_page = 0;
	}
	$threads_articles = apf_get_article_thread_banmian($_GET['bid'],$num_page);
}
include_once(TEMP_ROOT_PATH.'/edit_banmian.php');
?>
